package de.htw_saarland.webservices.uddi;

import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Holder;

import org.uddi.api_v3.AddPublisherAssertions;
import org.uddi.api_v3.AssertionStatusItem;
import org.uddi.api_v3.AuthToken;
import org.uddi.api_v3.BindingDetail;
import org.uddi.api_v3.BindingTemplate;
import org.uddi.api_v3.BusinessDetail;
import org.uddi.api_v3.BusinessEntity;
import org.uddi.api_v3.BusinessService;
import org.uddi.api_v3.CompletionStatus;
import org.uddi.api_v3.DeleteBinding;
import org.uddi.api_v3.DeleteBusiness;
import org.uddi.api_v3.DeletePublisherAssertions;
import org.uddi.api_v3.DeleteService;
import org.uddi.api_v3.DeleteTModel;
import org.uddi.api_v3.DiscardAuthToken;
import org.uddi.api_v3.GetAuthToken;
import org.uddi.api_v3.GetRegisteredInfo;
import org.uddi.api_v3.PublisherAssertion;
import org.uddi.api_v3.RegisteredInfo;
import org.uddi.api_v3.SaveBinding;
import org.uddi.api_v3.SaveBusiness;
import org.uddi.api_v3.SaveService;
import org.uddi.api_v3.SaveTModel;
import org.uddi.api_v3.ServiceDetail;
import org.uddi.api_v3.TModel;
import org.uddi.api_v3.TModelDetail;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIPublicationPortType;
import org.uddi.v3_service.UDDISecurityPortType;

@WebService(name="UDDIPublicationService")
public class UDDIPublicationNode implements UDDIPublicationPortType, UDDISecurityPortType {
	
	public UDDIPublicationNode() {
		super();
	}
	
	
	@Override
	public void addPublisherAssertions(AddPublisherAssertions body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*		
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
	
		List<PublisherAssertion> assertions = body.getPublisherAssertion();
		
		for (PublisherAssertion a : assertions) {
			port.savePublisherAssertion(a);
		}
		*/				
	}

	@Override
	public void deleteBinding(DeleteBinding body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*		
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getBindingKey();
		
		for (String key : keys) {
			port.deleteBinding(key);
		}
		*/
	}

	@Override
	public void deleteBusiness(DeleteBusiness body)
			throws DispositionReportFaultMessage {

		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getBusinessKey();
		
		for (String key : keys) {
			port.deleteBusiness(key);
		}
		*/
	}

	@Override
	public void deletePublisherAssertions(DeletePublisherAssertions body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<PublisherAssertion> assertions = body.getPublisherAssertion();
		
		for (PublisherAssertion a : assertions) {
			port.deletePublisherAssertion(a);
		}
		*/
	}

	@Override
	public void deleteService(DeleteService body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getServiceKey();
		
		for (String key : keys) {
			port.deleteService(key);
		}
		*/
	}

	@Override
	public void deleteTModel(DeleteTModel body)
			throws DispositionReportFaultMessage {

		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getTModelKey();
		
		for (String key : keys) {
			port.deleteService(key);
		}
		*/
	}

	@Override
	public List<AssertionStatusItem> getAssertionStatusReport(String authInfo,
			CompletionStatus completionStatus)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(authInfo);
		/*	
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		*/
				return null;		
	}

	@Override
	public List<PublisherAssertion> getPublisherAssertions(String authInfo)
			throws DispositionReportFaultMessage {

		checkAuthToken(authInfo);
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		return port.getPublisherAssertions();
		 */
				return null;
	}

	@Override
	public RegisteredInfo getRegisteredInfo(GetRegisteredInfo body)
			throws DispositionReportFaultMessage {

		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		return port.getRegisteredInfo(body.getInfoSelection());
		 */
				return null;
	}

	@Override
	public BindingDetail saveBinding(SaveBinding body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
				
		List<BindingTemplate> templates = body.getBindingTemplate();
		
		for (BindingTemplate template : templates) {
			port.saveBinding(template);
		}
		*/
		//TODO: Return a valid object
		return new BindingDetail();
	}

	@Override
	public BusinessDetail saveBusiness(SaveBusiness body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
			
		List<BusinessEntity> entities = body.getBusinessEntity();
		
		for (BusinessEntity entity : entities) {
			port.saveBusinessEntity(entity);
		}
		*/
		//TODO: Return a valid object
		return new BusinessDetail();
	}

	@Override
	public ServiceDetail saveService(SaveService body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
			
		List<BusinessService> services = body.getBusinessService();
		
		for (BusinessService service : services) {
			port.saveService(service);
		}
		*/
		//TODO: Return a valid object
		return new ServiceDetail();
	}

	@Override
	public TModelDetail saveTModel(SaveTModel body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
	
		List<TModel> models = body.getTModel();
		
		for (TModel model : models) {
			port.saveTModel(model);
		}
		*/
		//TODO: Return a valid object
		return new TModelDetail();
	}

	@Override
	public void setPublisherAssertions(String authInfo,
			Holder<List<PublisherAssertion>> publisherAssertion)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(authInfo);
		
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<PublisherAssertion> assertions = publisherAssertion.value;
		
		for (PublisherAssertion assertion : assertions) {
			//TODO: Delete publisher assertions?
			port.savePublisherAssertion(assertion);
		}
		 */
	}

	/**
	 * ************************************************************************
	 * Security API
	 * ************************************************************************
	 */
	
	/**
	 * Invalidate a created authentication token.
	 * Can be used to inform the registry that the token is no longer required.
	 */
	@Override
	public void discardAuthToken(DiscardAuthToken body)
			throws DispositionReportFaultMessage {
		//We don't create authentication tokens so there is nothing to do here.
	}

	/**
	 * Get an authentication token. 
	 * The token is required for using the other UDDI APIs.
	 */
	@Override
	public AuthToken getAuthToken(GetAuthToken body)
			throws DispositionReportFaultMessage {
		
		/*
		 * Here should be a sophisticated security concept, 
		 * however we don't need that in our project. 
		 * Everyone can publish service.
		 */
		String user = body.getUserID();
		String pass = body.getCred();
		
		if ((user == null) || (pass == null)) {
			return null;
		}
		
		StringBuffer authinfo = new StringBuffer(user + pass);
		authinfo.reverse();
		
		AuthToken token = new AuthToken();
		token.setAuthInfo(authinfo.toString());
		
		return token;
	}

	private void checkAuthToken(String token) 
		throws DispositionReportFaultMessage {
		
		if ((token == null) || (token.isEmpty())) {
			throw new DispositionReportFaultMessage("Authentication token invalid", null);
		}
	}	
	
	/**
	 * Create web service for testing purposes. Only works with Java6.
	 */
	public static void main(String[] args) {
		UDDIPublicationNode node = new UDDIPublicationNode();
		Endpoint.publish("http://localhost:8080/uddipublicationnode", node);

	}

}
